package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.IntoStorageBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.IntoStorageMapper;
import com.liuzu.esys.mapper.StorageMapper;
import com.liuzu.esys.pojo.IntoStorage;
import com.liuzu.esys.pojo.Storage;
import com.liuzu.esys.service.IntoStorageService;
import com.liuzu.esys.service.StorageService;
import com.liuzu.esys.vo.IntoStorageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 太上老君保佑：代码，绿绿绿绿绿！！！！！
 *
 * @Description :
 * @Author : 冯小冯
 * @Date : 2022-07-26 09:16
 */
@Service("/intoStorageService")
public class IntoStorageServiceImpl implements IntoStorageService {
    @Autowired
    private IntoStorageMapper intoStorageMapper;
    @Autowired
    private StorageService storageService;



    @Override
    public LayuiPageVo<IntoStorage> intoStorageService(Map<String, Object> params) {
        // mybatis 的 分页插件
        Integer page = Integer.parseInt(params.get("page")+"");
        Integer limit = Integer.parseInt(params.get("limit")+"");
        PageHelper.startPage(page,limit);

        List<IntoStorage> intoStorages = intoStorageMapper.selectByParams(params);
        // mybatis 的 分页数据结构
        PageInfo<IntoStorage> studentPageInfo = new PageInfo<>(intoStorages);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<IntoStorage> studentLayuiPageVo = new LayuiPageVo<>();
        studentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        studentLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        studentLayuiPageVo.setCount(studentPageInfo.getTotal()); // 总记录数
        studentLayuiPageVo.setData(studentPageInfo.getList());  // 分页的列表数据
        return studentLayuiPageVo;
    }

    @Override
    public int intoStorageAdd(IntoStorage intoStorage) {

        return intoStorageMapper.intoStorageAdd(intoStorage);
    }

    @Override
    public int intoStorageUpdate(IntoStorage intoStorage) {
        return intoStorageMapper.intoStorageUpdate(intoStorage);
    }

    @Override
    public int intoStorageDelete(int id) {
        return intoStorageMapper.intoStorageDelete(id);
    }

    @Override
    public List<IntoStorageBo> intoStorageExcelData(IntoStorage intoStorage) {

        IntoStorage intoStorage1 = new IntoStorage();

        List<IntoStorage> intoStorages = intoStorageMapper.selectByKey(intoStorage);
        List<IntoStorageBo> collect = intoStorages.stream().map((o) -> {
            IntoStorageBo intoStorageBo = new IntoStorageBo();

            intoStorageBo.setId(o.getId());
            intoStorageBo.setName(o.getName());
            intoStorageBo.setType(o.getType());
            intoStorageBo.setAddress(o.getAddress());
            intoStorageBo.setNumber(o.getNumber());
            intoStorageBo.setManger(o.getManger());
            intoStorageBo.setPhone(o.getPhone());
            intoStorageBo.setDate(o.getDate());
            return intoStorageBo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public int storageadd(IntoStorage intoStorage) {

        int i = intoStorageMapper.intoStorageAdd(intoStorage);
        Storage storage = new Storage();
        int j = storageService.storageaddByInto(intoStorage,storage);

       return j;

    }


    @Override
    public int deleteStoragesByIds(String ids) {
        String[] arrId = ids.split(",");
        return intoStorageMapper.deleteMessageByIds(arrId);

    }
    @Override
    public List<IntoStorageVo> datePieData() {

        List<IntoStorageVo> list = intoStorageMapper.datePieData();
        return list;
    }


}
